package cn.kgc.tangcco.servlet;

import cn.binarywang.tools.generator.ChineseIDCardNumberGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.binarywang.tools.generator.base.GenericGenerator;
import cn.kgc.tangcco.model.ResposeText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Emp;
import cn.kgc.tangcco.utils.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/11 7:19
 */
@WebServlet(urlPatterns = "/freemaker.action")
public class FreeMaker extends BaseServlet {
    private static final long serialVersionUID = -7521213702854991836L;
    private static List<Emp> emps = new ArrayList<>();

    static {
        GenericGenerator chineseNameGenerator = ChineseNameGenerator.getInstance();
        GenericGenerator chineseIDCardNumberGenerator = ChineseIDCardNumberGenerator.getInstance();

        for (int i = 0; i < 5; i++) {
            emps.add(new Emp(i + 1, chineseNameGenerator.generate(), chineseIDCardNumberGenerator.generate()));
        }
    }

    public void forwordFtl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("slogan", "桃李不言下自成蹊");
        forword(request, response, "/template/freemarker.ftl");
    }

    public void forwordTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("emps", emps);
        forword(request, response, "/template/table.ftl");
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String idCard = request.getParameter("idCard");
        ResposeText resposeText = new ResposeText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
        Emp emp = new Emp(emps.get(emps.size() - 1).getId() + 1, name, idCard);
        if (emps.add(emp)) {
            resposeText.setCode(ResultCode.SUCCESS.getCode());
            resposeText.setMsg(ResultCode.SUCCESS.getMsg());
        }
        printJson(response, resposeText);
        return;
    }

    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ids[]");
        ResposeText resposeText = new ResposeText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
        for (int i = 0; i < ids.length; i++) {
            int finalI = i;
            emps.removeIf(emp -> emp.getId() == Integer.parseInt(ids[finalI]));
        }
        resposeText.setCode(ResultCode.SUCCESS.getCode());
        resposeText.setMsg(ResultCode.SUCCESS.getMsg());
        printJson(response, resposeText);
        return;
    }

    public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("ids[]");
        ResposeText resposeText = new ResposeText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
        List<Emp> empList = emps.stream().filter(emp -> emp.getId() == Integer.parseInt(id)).collect(Collectors.toList());
        if (empList != null && empList.size() > 0) {
            printJson(response, new ResposeText<Emp>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), empList.get(0)));
            return;
        }
        printJson(response,resposeText);
        return;
    }
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("ids[]");
        String name = request.getParameter("name");
        String idCard = request.getParameter("idCard");
        ResposeText resposeText = new ResposeText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
        for (int i = 0; i < emps.size(); i++) {
            Emp emp = emps.get(i);
            if (emp.getId() == Integer.parseInt(id)){
                emp.setName(name);
                emp.setIdCard(idCard);
                emps.set(i,emp);
                printJson(response, new ResposeText<Emp>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), emp));
                return;
            }
        }
        printJson(response,resposeText);
        return;
    }
}
